package com.karolak.stock.strategy;

import java.text.DecimalFormat;

import com.karolak.stock.data.DataManager;
import com.karolak.stock.indicator.Indicator;

/**
 * 
 * @author karol
 * 
 */
public class Collector extends Indicator {

	private int pos;
	private double price;
	private double prof;
	private double trProf;
	private int trCount;
	private int intervalCounter;

	public Collector(String name, DataManager dataManager, int period, Voter voter) {
		super(dataManager, period, voter);
		setName(name);
	}

	public double execute(double x) {
		setIntervalCounter(getIntervalCounter() + 1);
		double v0 = getDataManager().getLast(getInput().getName());
		if (v0 > 0 && getPos() <= 0) {
			longuj(x);
		} else if (v0 < 0 && getPos() >= 0) {
			shortuj(x);
		} else {
			// DO NOTHING, keep
		}
		getDataManager().set("Position", getPos());
		return getProf();
	}

	protected void longuj(double x) {
		int pos = getPos();
		double price = getPrice();
		if (pos < 0) {
			setTrProf(price - x);
			setProf(getProf() + getTrProf());
		}
		setPos(1);
		setPrice(x);
		setTrCount(getTrCount() + 1);
		getDataManager().set("HighTrPrice", x);
		getDataManager().set("LowTrPrice", x);
		log(x);
	}

	protected void shortuj(double x) {
		int pos = getPos();
		double price = getPrice();
		if (pos > 0) {
			setTrProf(x - price);
			setProf(getProf() + getTrProf());
		}
		setPos(-1);
		setPrice(x);
		setTrCount(getTrCount() + 1);
		getDataManager().set("HighTrPrice", x);
		getDataManager().set("LowTrPrice", x);
		log(x);
	}

	protected void log(double last) {
		// NumberFormat df3 = DecimalFormat.getInstance();
		// df3.setMaximumFractionDigits(4);
		DecimalFormat df = new DecimalFormat("00000");
		DecimalFormat df2 = new DecimalFormat(" 0;-0");
		DecimalFormat df3 = new DecimalFormat(" 0.00000;-0.00000");
		DecimalFormat df4 = new DecimalFormat(" 0.00;-0.00");
		DecimalFormat df5 = new DecimalFormat("0.00000");

		// df.set
		double v0 = getDataManager().getLast(getInput().getName());
		StringBuilder sb = new StringBuilder();
		sb.append("ct:").append(df.format(getIntervalCounter()));
		sb.append(", last:").append(df5.format(last));
		sb.append(", pos:").append(df2.format(getPos()));
		sb.append(", price:").append(df5.format(getPrice()));
		sb.append(", trProf:").append(df3.format(getTrProf()));
		sb.append(", prof:").append(df3.format(getProf()));
		sb.append(",tr:").append(df.format(getTrCount()));
		sb.append(", vote:").append(df4.format(v0));
		sb.append(", voteResults:").append(getVoter().getResults());
		System.out.println(sb);
	}

	protected int getPos() {
		return pos;
	}

	protected void setPos(int pos) {
		this.pos = pos;
	}

	protected double getPrice() {
		return price;
	}

	protected void setPrice(double price) {
		this.price = price;
	}

	protected double getProf() {
		return prof;
	}

	protected void setProf(double prof) {
		this.prof = prof;
	}

	protected int getTrCount() {
		return trCount;
	}

	protected void setTrCount(int trCount) {
		this.trCount = trCount;
	}

	public double getTrProf() {
		return trProf;
	}

	public void setTrProf(double trProf) {
		this.trProf = trProf;
	}

	protected Voter getVoter() {
		return (Voter) getInput();
	}

	protected int getIntervalCounter() {
		return intervalCounter;
	}

	protected void setIntervalCounter(int intervalCounter) {
		this.intervalCounter = intervalCounter;
	}
}
